package top.yuhaitao.cloud.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import top.yuhaitao.cloud.entity.User;

@FeignClient(name = "springboot-cloud-provider-user", fallback = HystrixClientFallback.class)
public interface UserFeignClient {

	@RequestMapping(value = "/getUserById", method = RequestMethod.GET)
	public User getUserById(@RequestParam("id") Long id);

	@RequestMapping(value = "/getUserById2/{id}", method = RequestMethod.GET)
	public User getUserById2(@PathVariable("id") Long id); // 两个坑：1. @GetMapping不支持   2. @PathVariable得设置value

	@RequestMapping(value = "/user", method = RequestMethod.POST)
	public User postUser(@RequestBody User user);

	// 该请求不会成功，只要参数是复杂对象，即使指定了是GET方法，feign依然会以POST方法进行发送请求。可能是我没找到相应的注解或使用方法错误。
	@RequestMapping(value = "/getUser", method = RequestMethod.GET)
	public User getUser(User user);
}

// 可以单独提出携程一个类
@Component
class HystrixClientFallback implements UserFeignClient {

	@Override
	public User getUserById(Long id) {
		User user = new User();
		user.setId(0L);
		user.setName("feign hystix");
		return user;
	}

	@Override
	public User getUserById2(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User postUser(User user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User getUser(User user) {
		// TODO Auto-generated method stub
		return null;
	}

}